import 'package:flutter/material.dart';
import '../generated/l10n.dart';
import '../service/one_sentence.dart';
import 'settings_page.dart';

import '../wights/LeftSlideInButton.dart';


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  OneSentence instance = OneSentence();
  late String? sentence = '点一下就会有一句话，点一下试试吧';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween, // 关键：左右两端对齐
          children: [
            Text(S.of(context).home), // 标题在左
            LeftSlideInButton(myPageBuilder: (context) => SettingsPage(), icon: Icon(Icons.settings)),
          ],
        ),
      ),
      body: Column(
        crossAxisAlignment:  CrossAxisAlignment.center,
        children: [
          Center(
            child: Card(
              margin: EdgeInsets.all(16), // 设置卡片的外边距
              child: Padding(
                padding: EdgeInsets.all(16), // 设置卡片内部的内边距
                child: Column(
                  children: [
                    SizedBox(height: 10),
                    RichText(
                      text: TextSpan(
                        children: [
                          WidgetSpan(
                            child: SizedBox(width: 40), // 通过SizedBox的宽度来模拟首行缩进
                          ),
                          TextSpan(
                            text: sentence!,
                            style: TextStyle(
                              fontSize: 20,
                              color: Colors.grey[600],
                            ),
                          ),
                        ],
                      ),
                    ),
                    TextButton.icon(
                      onPressed: () async {
                        String? sentenceResult = await instance.getSentence();
                        if (sentenceResult != null) {
                          setState(() {
                            sentence = sentenceResult;
                          });
                        }
                      },
                      label: Text("再来一句"),
                      icon: Icon(Icons.generating_tokens_outlined),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
